// pages/finance_account/finance_index.js
import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    userInfo: {},
    uploadUrl: wx.getStorageSync('uploadUrl'),

    recordList: [],

    noWallet: true,

    pageNo: 1,
    hasNextPage: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfoList()
    this.getRecordList();
  },

  // 去提现
  goDrawOutCash(event) {
    wx.setStorageSync('accId', event.currentTarget.dataset.accid)
    wx.navigateTo({
      url: '/pages/finance_account/draw_out_cash/draw_out_cash',
    })
  },

  // 去编辑
  handleEdit(event) {
    wx.setStorageSync('editList', this.data.infoList)
    wx.navigateTo({
      url: '/pages/finance_account/finance_create/finance_create?handleId=' + event.currentTarget.dataset.id + '&isEdit=true',
    })
  },

  // 去创建资金账户
  goCreateFinanceAccount(e) {
    if (e.currentTarget.dataset.id) {
      wx.navigateTo({
        url: '/pages/finance_account/finance_create/finance_create?id=' + e.currentTarget.dataset.id,
      })
      return
    }
    wx.navigateTo({
      url: '/pages/finance_account/finance_create/finance_create',
    })
  },

  // 获取用户资金账户列表
  getInfoList() {
    var url = '/odr/protected/account/getMyAccount';

    //请求
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'GET',
    }, (res) => {
      console.log(res)
      if (res.vo !== null) {
        this.data.noWallet = false
        this.setData({
          infoList: res.vo,
          noWallet: this.data.noWallet
        })
      } else {
        this.setData({
          noWallet: true
        })
      }
    })
  },

  // 获取资金记录列表
  getRecordList() {
    var url = '/odr/protected/account/findMyStatement'
    var params = {
      pageNo: this.data.pageNo || 1,
      pageSize: 10,
    }

    //请求
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: params,
    }, (res) => {
      console.log(res)
      this.data.recordList = res.list
      this.setData({
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
        recordList: this.data.recordList
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo') || {}
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})